package com.wedo.admin.controller;

import com.wedo.admin.config.JwtProperties;
import com.wedo.admin.pojo.Admin;
import com.wedo.admin.service.AdminService;
import com.wedo.auth.pojo.UserInfo;
import com.wedo.auth.utils.JwtUtils;
import com.wedo.common.pojo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private JwtProperties jwtProperties;

    @GetMapping("queryByAdminName")
    public ResponseEntity<Admin> queryByAdminName(@RequestParam("adminName")String adminName, @RequestParam("password")String password) {
        Admin result = this.adminService.queryByAdminName(adminName,password);
        return ResponseEntity.ok(result);
    }

    @PostMapping("addAdmin")
    public ResponseEntity<Boolean> addAdmin(@RequestHeader("WD_TOKEN") String token, @RequestBody Admin admin) {
        if (StringUtils.isBlank(admin.getAdminName()) || StringUtils.isBlank(admin.getPassword()) ||StringUtils.isBlank(token)) {
            return ResponseEntity.badRequest().build();
        }
        if (!authJwt(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        if (!this.adminService.addAdmin(admin)) {
            return ResponseEntity.ok(false);
        }

        return ResponseEntity.ok(true);
    }

    @GetMapping("page")
    public ResponseEntity<PageResult<Admin>> queryAdminsByPage(
            @RequestParam(value = "adminName",required = false) String adminName,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "6") Integer rows
    ) {
        PageResult<Admin> result = this.adminService.queryAdmins(adminName,page,rows);
        return ResponseEntity.ok(result);
    }

    @PutMapping
    public ResponseEntity<Boolean> updateAdminPassword(
            @RequestHeader("WD_TOKEN") String token,
            @RequestBody Admin admin
    ) {
        if (!authJwt(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        if (admin.getId() == null || StringUtils.isBlank(admin.getPassword())) {
            return ResponseEntity.badRequest().build();
        }
        Boolean result = this.adminService.updateAdminPassword(admin);
        return ResponseEntity.ok(result);
    }

    private boolean authJwt(String token) {
        UserInfo userinfo = null;
        // 解析jwt
        try {
            userinfo = JwtUtils.getInfoFromToken(token, this.jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        if (!"ADMIN".equals(userinfo.getRole())) {
            return false;
        }
        return true;
    }

    @DeleteMapping
    public ResponseEntity<Admin> deleteAdminByAid(
            @RequestHeader("WD_TOKEN") String token,
            @RequestBody Admin admin
    ) {
        if (!authJwt(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        if (admin.getId() == null) {
            return ResponseEntity.badRequest().build();
        }
        Admin result = this.adminService.deleteAdmin(admin);
        return ResponseEntity.ok(result);
    }
}
